using System;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Networking;

[RequireComponent(typeof(NetworkIdentity))]
public class ObjectPlacing : NetworkBehaviour
{
	[SyncVar]
	public string ItemID = string.Empty;

	[SyncVar]
	public string ItemData = string.Empty;

	[SyncVar]
	public string ItemUID = string.Empty;

	private static int kRpcRpcTellAllItemData;

	public string NetworkItemID
	{
		get
		{
			return ItemID;
		}
		[param: In]
		set
		{
			SetSyncVar(value, ref ItemID, 1u);
		}
	}

	public string NetworkItemData
	{
		get
		{
			return ItemData;
		}
		[param: In]
		set
		{
			SetSyncVar(value, ref ItemData, 2u);
		}
	}

	public string NetworkItemUID
	{
		get
		{
			return ItemUID;
		}
		[param: In]
		set
		{
			SetSyncVar(value, ref ItemUID, 4u);
		}
	}

	public void Start()
	{
	}

	public override void OnStartClient()
	{
		if (base.isServer)
		{
			CallRpcTellAllItemData(ItemID, ItemData, ItemUID);
		}
		base.OnStartClient();
	}

	[ClientRpc(channel = 2)]
	private void RpcTellAllItemData(string itemID, string itemData, string itemUID)
	{
		NetworkItemID = itemID;
		NetworkItemData = itemData;
		NetworkItemUID = itemUID;
		SendMessage("OnPlacingReady", SendMessageOptions.DontRequireReceiver);
	}

	public void SetItemID(string id)
	{
		NetworkItemID = id;
	}

	public void SetItemUID(string uid)
	{
		NetworkItemUID = uid;
	}

	public void SetItemData(string data)
	{
		NetworkItemData = data;
	}

	public string GetUniqueID()
	{
		System.Random random = new System.Random();
		DateTime d = new DateTime(1970, 1, 1, 8, 0, 0, DateTimeKind.Utc);
		double totalSeconds = (DateTime.UtcNow - d).TotalSeconds;
		return $"{Convert.ToInt32(totalSeconds):X}" + "-" + $"{Convert.ToInt32(Time.time * 1000000f):X}" + "-" + $"{random.Next(1000000000):X}";
	}

	private void UNetVersion()
	{
	}

	protected static void InvokeRpcRpcTellAllItemData(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("RPC RpcTellAllItemData called on server.");
		}
		else
		{
			((ObjectPlacing)obj).RpcTellAllItemData(reader.ReadString(), reader.ReadString(), reader.ReadString());
		}
	}

	public void CallRpcTellAllItemData(string itemID, string itemData, string itemUID)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("RPC Function RpcTellAllItemData called on client.");
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)2);
		networkWriter.WritePackedUInt32((uint)kRpcRpcTellAllItemData);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		networkWriter.Write(itemID);
		networkWriter.Write(itemData);
		networkWriter.Write(itemUID);
		SendRPCInternal(networkWriter, 2, "RpcTellAllItemData");
	}

	static ObjectPlacing()
	{
		kRpcRpcTellAllItemData = -1796717454;
		NetworkBehaviour.RegisterRpcDelegate(typeof(ObjectPlacing), kRpcRpcTellAllItemData, InvokeRpcRpcTellAllItemData);
		NetworkCRC.RegisterBehaviour("ObjectPlacing", 0);
	}

	public override bool OnSerialize(NetworkWriter writer, bool forceAll)
	{
		if (forceAll)
		{
			writer.Write(ItemID);
			writer.Write(ItemData);
			writer.Write(ItemUID);
			return true;
		}
		bool flag = false;
		if ((base.syncVarDirtyBits & 1) != 0)
		{
			if (!flag)
			{
				writer.WritePackedUInt32(base.syncVarDirtyBits);
				flag = true;
			}
			writer.Write(ItemID);
		}
		if ((base.syncVarDirtyBits & 2) != 0)
		{
			if (!flag)
			{
				writer.WritePackedUInt32(base.syncVarDirtyBits);
				flag = true;
			}
			writer.Write(ItemData);
		}
		if ((base.syncVarDirtyBits & 4) != 0)
		{
			if (!flag)
			{
				writer.WritePackedUInt32(base.syncVarDirtyBits);
				flag = true;
			}
			writer.Write(ItemUID);
		}
		if (!flag)
		{
			writer.WritePackedUInt32(base.syncVarDirtyBits);
		}
		return flag;
	}

	public override void OnDeserialize(NetworkReader reader, bool initialState)
	{
		if (initialState)
		{
			ItemID = reader.ReadString();
			ItemData = reader.ReadString();
			ItemUID = reader.ReadString();
			return;
		}
		int num = (int)reader.ReadPackedUInt32();
		if ((num & 1) != 0)
		{
			ItemID = reader.ReadString();
		}
		if ((num & 2) != 0)
		{
			ItemData = reader.ReadString();
		}
		if ((num & 4) != 0)
		{
			ItemUID = reader.ReadString();
		}
	}
}
